var layer = layui.layer;
var form = layui.form;
var flag = false;
$(function () {
    layer.msg("欢迎！！！");
    let $name = $("#name");
    $name.empty();
    let item = sessionStorage.getItem("login");
    let parse = JSON.parse(item);
    let username = parse.username;
    $name.append(username);
})
function base_msg() {
    layer.open({
        type: 1,
        title: "个人信息",
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '240px'], //宽高
        content: '<div class="layui-card">\n' +
            '  <div class="layui-card-header">卡片面板</div>\n' +
            '  <div class="layui-card-body">\n' +
            '    卡片式面板面板通常用于非白色背景色的主体内<br>\n' +
            '    从而映衬出边框投影\n' +
            '  </div>\n' +
            '</div>'
    });
}

function update_pwd() {
    layer.open({
        type: 1,
        title: "密码修改",
        skin: 'layui-layer-rim', //加上边框
        area: ['520px', '360px'], //宽高
        content:'<hr class="layui-bg-black"><input style="width: 65%;margin: 0 auto" type="password" name="oldpwd" required lay-verify="required" placeholder="请输入旧密码" autocomplete="off" class="layui-input" id="oldpwd" onblur="oldpwd()">' +
            '<hr class="layui-bg-black"><input style="width: 65%;margin: 0 auto" type="password" name="newpwd1" required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input" id="newpwd1" onblur="pwd()">' +
            '<hr class="layui-bg-black"><input style="width: 65%;margin: 0 auto" type="password" name="newpwd2" required lay-verify="required" placeholder="请重复新密码" autocomplete="off" class="layui-input" id="newpwd2" onblur="pwd()">' +
            '<div align="center"><button class="layui-btn layui-btn-radius layui-btn-primary" onclick="changepwd()">提交</button></div>'
        ,success: function(){
            form.render();

        }
    })
}
//新密码验证
function pwd() {
    let newpwd1 = $("#newpwd1").val();
    let newpwd2 = $("#newpwd2").val();
    if(newpwd1!==newpwd2){
        $("#newpwd1").css("border-color", "red")
        $("#newpwd2").css("border-color", "red")
    }else {
        $("#newpwd2").css("border-color","green")
        $("#newpwd1").css("border-color","green")
        flag = true;
    }
}
//旧密码验证
function oldpwd() {
    let item = sessionStorage.getItem("login");
    let parse = JSON.parse(item);
    if (parse.password===$("#oldpwd").val()){
        $("#oldpwd").css("border-color","green");
        flag = true;
    }else {
        $("#oldpwd").css("border-color","red");
        flag = false;
    }
}
//修改密码
function changepwd() {
    let item = sessionStorage.getItem("login");
    let parse = JSON.parse(item);
    let email = parse.email;
    let password = $("#newpwd2").val();
    if ($("#oldpwd").val()===""||password===""){
        flag = false;
    }
    if (flag){
        $.post("/updatepwd",{"email":email,"password":password},function (data) {
            if (data.code===200){
                window.location.href="/html/admin.html";
            }else {
                layer.msg(data.info)
            }
        },"json")
    }else{
        layer.msg("密码不能为空！")
    }
}
//注销
function logout() {
    $.post("/logout",function (data) {
        if (data.code===200){
            sessionStorage.removeItem("login");
            window.location.href="/index.html";
        }else {
            layer.msg("注销失败");
        }
    },"json")
}